<script setup lang="ts">
import { useRoute } from 'vue-router'
import { getCurrentInstance, ref, watch, watchEffect, onMounted } from 'vue'
const route = useRoute()
const { proxy } = getCurrentInstance()

/* ---- */
const logUser = proxy.$Store.get('logUser') || {}
const WXConfig = proxy.$Store.get('WXConfig') || {}

/* --设置微信分享功能-- */
proxy.$WxTool.loadJsdk(WXConfig, (obj) => {
  if (!obj) return
  // ----
  obj.hideMenuItem()
})

/* ---- */
const animName = ref()
const user = {
  telphone: '',
  // ----
  forSelf: Number(logUser.id) === Number(proxy.$route.query.dealer_id)
}
// ----
const curIndex = ref()
const curTabs = ref()
// ----
const items = ref([])

/* ---- */
const layer = ref()

/* ---- */
const getCurrencyInfo = async () => {
  // --获取信息--
  const forUserData = { params: { 'DealerSearch[id]': proxy.$route.query.dealer_id } }
  await proxy.$Axios.all([
    proxy.$Axios.get('dealer/index', forUserData)
  ]).then(proxy.$Axios.spread((userData) => {
    const UData = userData.items[0] || {}
    user.telphone = UData.tag || UData.telphone
  })).catch((err) => { proxy.$Myfun.forToast(layer.value, 'open', err) })
  // ----
  items.value = [{
    show: true,
    name: '选购商品',
    icon: 'fa fa-cloud-check',
    callback: () => { proxy.$Myfun.openPage(proxy, 'push', 'CloudHome', { dealer_id: proxy.$route.query.dealer_id }) }
  }, {
    show: true,
    name: '咨询洽谈',
    icon: 'fa fa-qrcode',
    callback: () => { window.location.href = 'tel:' + user.telphone }
  }, {
    show: true,
    name: '上门采购',
    icon: 'fa fa-compass',
    callback: () => { proxy.$Myfun.openPage(proxy, 'push', 'CloudMap', { dealer_id: proxy.$route.query.dealer_id }) }
  }, {
    show: true,
    name: '我的订单',
    icon: 'fa fa-sensor-cloud',
    callback: () => {
      if (user.forSelf) {
        proxy.$Myfun.openPage(proxy, 'push', 'UserCloudOrder', { dealer_id: proxy.$route.query.dealer_id })
      } else {
        proxy.$Myfun.openPage(proxy, 'push', 'OwnCloudOrder', { dealer_id: proxy.$route.query.dealer_id })
      }
    }
  }]
}

/* ---- */
watch(() => route.meta, (to, from) => {
  const toIndex = to.index
  const fromIndex = from.index || toIndex
  if (Number(toIndex) >= Number(fromIndex)) {
    animName.value = 'slide-left'
  } else {
    animName.value = 'slide-right'
  }
})
watchEffect(() => {
  animName.value = 'slide-left'
  curIndex.value = Number(proxy.$Forindex)
  curTabs.value = route.meta.tabs || false
})

/* ---- */
onMounted(() => {
  getCurrencyInfo()
})
</script>

<template>
  <div>
    <router-view v-slot="{ Component }">
      <transition :name="animName">
        <component :is="Component" />
      </transition>
    </router-view>
    <tabs-view :cur-index="curIndex" :cur-tabs="curTabs" :items="items"/>
    <layer-view ref="layer"/>
  </div>
</template>
